package cn.imvc.app.controllers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.domains.AppResult;
import cn.imvc.app.entities.Account;
import cn.imvc.app.entities.Favorites;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.FavoritesModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.core.Render;
import cn.imvc.util.PageableUtils;

@Controller
@RequestMapping("/favorites")
public class FavoritesControllerImpl implements FavoritesController {
	@Autowired
	private MemberModel memberModel;
	@Autowired
	private FavoritesModel favoritesModel;
	@RequestMapping("/save.html")
	public @ResponseBody Object saveFavorite(Integer skuId,Integer goodsId,String token){
		Member member = memberModel.getLoginMember(token);
		if(member!=null){
			Favorites favorited = favoritesModel.getFavoriteByMemberIdAndSkuId(member.getId(), skuId);
			if(favorited!=null){
				return new AppResult("2","该商品已经收藏过");
			}
			favoritesModel.saveFavorite(skuId,goodsId,member);
			return new AppResult("1","收藏成功");
		}else{
			return new AppResult("-1","请先登录");
		}
	}
	

	@RequestMapping("/favorites.html")
	public String favorites(String token) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.html";
		}	

		return "favorites/favorites";
	}

	@RequestMapping("/favorites.htm")
	public String pcfavorites(Model model,String token) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.htm";
		}
		return "favorites/pcfavorites";
	}
	
	@RequestMapping("/getFavorites.html")
	public String getFavorites(Model model,String token,Integer page) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.html";
		}		
		Member member = memberModel.getLoginMember(token);
		PageRequest pageRequest=PageableUtils.bulidPageRequest(page,10,"createdAt_DESC",true);
		Page<Favorites> favorites=favoritesModel.findByMemberId( member.getId(),pageRequest);
		model.addAttribute("favorites", favorites.getContent());
		if(favorites.getTotalPages()==(page+1)){
			model.addAttribute("over",true);
		}else{
			model.addAttribute("over",false);
		}
		return "favorites/favorites-list";
	}
	
	@RequestMapping("/getFavorites.htm")
	public String pcgetFavorites(Model model,String token,Integer page) {
		page--;
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.htm";
		}		
		Member member = memberModel.getLoginMember(token);
		PageRequest pageRequest=PageableUtils.bulidPageRequest(page,6,"createdAt_DESC",true);
		Page<Favorites> favorites=favoritesModel.findByMemberId( member.getId(),pageRequest);
		model.addAttribute("favorites", favorites.getContent());
		model.addAttribute("count", favorites.getTotalPages());
		return "favorites/pcfavorites-list";
	}
	
	@RequestMapping("deleteFavorites.json")
	@ResponseBody
	public Map<String, Object> balancePay(String token, String code,String ids) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			resultMap.put("code", "-1");
			resultMap.put("msg", "未登录");
			return resultMap;
		}
		
		if (ids==null||"".equals(ids)){
			resultMap.put("code", "0");
			resultMap.put("msg", "请选择要删除的收藏");
			return resultMap;
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		try{
		favoritesModel.deleteFavorites(ids,member);
		}catch(Exception e){
			resultMap.put("code", "0");
			resultMap.put("msg", "您没有权限这么做");

			return resultMap;
		}
		
		resultMap.put("code", "1");
		resultMap.put("msg", "删除成功");

		return resultMap;
	}
	
}
